using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace Catastrophe.AutoSulution.TaskM
{
	public class MyTask
	{
		private object _instance;

		/// <summary>
		/// 执行初始化方法一次标识
		/// </summary>
		private bool _runInitializeOnce = true;

		/// <summary>
		/// 当前任务
		/// </summary>
		public Task TaskNow { get; set; }

		/// <summary>
		/// 通知一个或多个正在等待的线程已发生事件
		/// </summary>
		public ManualResetEvent ManualReset { get; set; }

		/// <summary>
		/// 取消任务对象
		/// </summary>
		public CancellationTokenSource TokenSource { get; set; }

		/// <summary>
		/// 方法名称
		/// </summary>
		public string MethodName { get; set; }

		/// <summary>
		/// 功能描述
		/// </summary>
		public string Function { get; set; }

		/// <summary>
		/// 开始时间
		/// </summary>
		public DateTime StartTime { get; set; }

		/// <summary>
		/// 实时周期记录缓存
		/// </summary>
		public Queue<long> RecordCycleBuffer { get; set; }

		/// <summary>
		/// 当前周期实时时间
		/// </summary>
		public long CycleValueNow { get; set; }

		/// <summary>
		/// 异常
		/// </summary>
		public Exception Exception { get; set; }

		/// <summary>
		/// 初始化只执行一次方法
		/// </summary>
		public MethodInfo InitializeMethod { get; set; }

		public MyTask()
		{
		}

		public MyTask(object instance)
		{
			this._instance = instance;
		}

		/// <summary>
		/// 执行初始化方法
		/// </summary>
		public void ExcuteInitialize()
		{
			if (this.InitializeMethod != null && this._instance != null && this._runInitializeOnce)
			{
				try
				{
					this.InitializeMethod.Invoke(this._instance, null);
					this._runInitializeOnce = false;
				}
				catch (Exception ex)
				{
					Exception ex3 = (this.Exception = ex);
				}
			}
		}

		/// <summary>
		/// 复位初始化运行一次的标识，初始化方法会再次运行
		/// </summary>
		public void ResetRunOnceFlag()
		{
			this._runInitializeOnce = true;
		}
	}
}
